package com.solution.wx.my.controller.finance;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.solution.admin.finance.wallet.entity.StatMonthWalletFlowEntity;
import com.solution.admin.finance.wallet.entity.WalletEntity;
import com.solution.admin.finance.wallet.entity.WalletFlowEntity;
import com.solution.admin.finance.wallet.service.WalletFlowService;
import com.solution.admin.finance.wallet.service.WalletService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * PC端个人中心-客户钱包
 * @author lufer
 * @date 2018-05-02 17:18:01
 */
@RequestMapping("/wx/my/wxWallet")
@Controller
public class WxWalletController extends BaseController {

	//private Logger logger = LoggerFactory.getLogger(WxWalletController.class);

	@Autowired
	private WalletService walletService;
	@Autowired
	private WalletFlowService walletFlowService;
	
	/**
	 * 跳转微信端我的钱包界面
	 * @return string 跳转页面路径
	 * @author lufer
 	 * @date 2018-05-02 17:18:01
	 */
	@RequestMapping("/toMyWallet")
	public String toMyWallet(HttpServletRequest request, Model model) {
		
		/***************查询某一客户财富汇总****************/
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//从session中获取到当前登录客户ID
		int customerId = (int) getCustomerId();
		//封装查询参数
		paramMap.put("customerId", customerId);
		//查询当前登录客户的财富
		List<WalletEntity> walletList = walletService.listWallet(paramMap);
		if(walletList == null || walletList.size() == 0 ){
			WalletEntity wallet = new WalletEntity();
			wallet.setTotalMoney(new BigDecimal(0));
			wallet.setGetableMoney(new BigDecimal(0));
			wallet.setGetingMoney(new BigDecimal(0));
			walletList.add(wallet);
		}
		model.addAttribute("walletList",walletList);
		
		/****************查询按月份统计钱包流水汇总*******************/
		//分页相关
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(1);
		//每页显示一条统计数据
		pageParam.setNumPerPage(1);
		
		//查询参数（查询月份）
		String month = request.getParameter("month");
		//查询参数（支出类型）
		String payType = request.getParameter("payType");
		Map<String, Object> statParamMap = new HashMap<String, Object>();
		//按月份统计出当前所属用户流水汇总
		//封装查询参数
		statParamMap.put("customerId", customerId);
		statParamMap.put("month", month);
		statParamMap.put("payType", payType);
		PageBean pageBean = walletFlowService.listStatMonthWalletFlow(pageParam,statParamMap);
		List<Object> statList = pageBean.getRecordList();
		model.addAttribute("statList",statList);
		model.addAttribute("pageBean", pageBean);
		
		/********************按月份统计钱包流水记录***********************/
		// 查询某一客户在某一月份中的钱包流水记录
		if(null != statList){
			Iterator<Object> ite = statList.iterator();
			if(ite.hasNext()){
				StatMonthWalletFlowEntity statMonthWalletFlowEntity = (StatMonthWalletFlowEntity) ite.next();
				// 查询根据月份查询某一客户钱包流水记录
				if(null != statMonthWalletFlowEntity){
					Map<String, Object> parMap = new HashMap<String, Object>();
					//封装查询参数
					parMap.put("customerId", customerId);//客户编号
					parMap.put("month", statMonthWalletFlowEntity.getMonth());//月份
					parMap.put("payType", payType);//支出类型
					
					List<WalletFlowEntity> walletFlowList = walletFlowService.listWalletFlowByMonth(parMap);
					if(null != walletFlowList){
						model.addAttribute("walletFlowList",walletFlowList);
					}
				}
			}
		}
		//把参数回传到请求页面
		model.addAttribute("payType", payType);
		//跳转页面地址
		return "wx/my/wallet/wallet";
	}
	
	/**
	 * 微信端查询某一客户钱包流水记录
	 * 作者：lufer
	 * 时间：2018年5月19日 上午11:21:32
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/searchWalletFlow")
	public String searchWalletFlow(HttpServletRequest request , HttpServletResponse response){
		//当前显示第几页
		int pageNum = Integer.parseInt(request.getParameter("page").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		//每页显示一条流水统计数据（统计数据是按月份统计，每页显示一个月的钱包流水记录）
		pageParam.setNumPerPage(1);
		
		//查询参数集合
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//获取查询参数信息
		//查询月份
		String month = request.getParameter("month");
		//查询支出类型
		String payType = request.getParameter("payType");
		
		//从session中获取到当前登录客户ID
		int customerId = (int) getCustomerId();
		paramMap.put("customerId", customerId);
		paramMap.put("month", month);
		paramMap.put("payType", payType);
		
		//按月份统计出当前所属用户流水汇总
		PageBean pageBean = walletFlowService.listStatMonthWalletFlow(pageParam,paramMap);
		List<Object> statList = pageBean.getRecordList();
		//把统计数据封装成json格式传递到请求页面
		JSONObject responseJson = new JSONObject();
		responseJson.put("statList",JSONObject.toJSON(statList));
		// 查询某一客户在某一月份中的钱包流水记录
		if(null != statList){
			Iterator ite = statList.iterator();
			if(ite.hasNext()){
				StatMonthWalletFlowEntity statMonthWalletFlowEntity = (StatMonthWalletFlowEntity) ite.next();
				// 查询根据月份查询某一客户钱包流水记录
				if(null != statMonthWalletFlowEntity){
					Map<String, Object> parMap = new HashMap<String, Object>();
					//客户编号
					parMap.put("customerId", customerId);
					parMap.put("month", month);
					parMap.put("payType", payType);
					//查询月份
					parMap.put("month", statMonthWalletFlowEntity.getMonth());
					List<WalletFlowEntity> walletFlowList = walletFlowService.listWalletFlowByMonth(parMap);
					if(null != walletFlowList){
						responseJson.put("walletFlowList",JSONObject.toJSON(walletFlowList));
					}
				}
			}
		}
		try {
			response.getWriter().write(responseJson.toString());
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "wx/my/wallet/wallet";
	}

}
